package com.sae.vote.controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.sae.vote.entity.Subject;
import com.sae.vote.entity.Users;
import com.sae.vote.service.SubjectService;
import com.sae.vote.service.UserService;
import com.sae.vote.util.DbUtilsTemplate;

/**
 * 首页类，包括首页展现，用户登陆登出操作
 *
 */
@Controller
@RequestMapping
public class IndexController {

	@Autowired
	private UserService userService;

	@Autowired
	private SubjectService subjectService;

	/**
	 * 首页
	 * 
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(HttpServletRequest request) throws SQLException {
		// 获取第一页的数据
		List<Subject> subjects = subjectService.getSubjectsPaged(0,
				DbUtilsTemplate.PAGE_SIZE);
		if (subjects.size() == DbUtilsTemplate.PAGE_SIZE) {
			// 将下一页的页码放入页面
			request.setAttribute("page", 2);
		}
		request.setAttribute("subjects", subjects);
		// 将数据渲染到 index的页面
		return "index";
	}

	/**
	 * 分页数据（可以与index方法合并，分开只是为了结构更清晰）
	 * 
	 * @param page
	 *            页码
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/page/{page}", method = RequestMethod.GET)
	public String index(@PathVariable("page") int page,
			HttpServletRequest request) throws SQLException {
		// 通过页码获得该页的数据
		List<Subject> subjects = subjectService.getSubjectsPaged((page - 1)
				* DbUtilsTemplate.PAGE_SIZE, DbUtilsTemplate.PAGE_SIZE);
		if (subjects.size() == DbUtilsTemplate.PAGE_SIZE) {
			// 将下一页的页码放入页面
			request.setAttribute("page", page + 1);
		}
		request.setAttribute("subjects", subjects);
		// 将数据渲染到 index的页面
		return "index";
	}

	/**
	 * 登陆网站
	 * 
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @param session
	 *            将用户信息存入SAE的分布式session中，分布式session配置在web.xml
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/", method = RequestMethod.POST)
	public String login(@RequestParam("username") String username,
			@RequestParam("password") String password, HttpSession session)
			throws SQLException {
		// 通过用户名，密码判断用户登陆是否成功
		Users user = userService.checkLogin(username, password);
		// 判断用户是否存在并且状态是否为有效状态
		if (user != null && user.getState() == (byte) 1) {
			// 将用户信息存入session中
			session.setAttribute("user", user);
		}
		// 跳转至首页
		// TODO : 应该跳转至用户当前操作页
		return "redirect:/";
	}

	/**
	 * 登出操作
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/out")
	public String out(HttpSession session) {
		// 将用户信息从session中移除
		session.removeAttribute("user");
		return "redirect:/";
	}

}
